package com.interfaces;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.web.CommonJspKeys;
import com.common.web.CommonKeys;
import com.core.sysLog.bo.SysLogBo;
import com.core.sysLog.dto.SysLogDto;
import com.core.sysLog.dto.SysLogSearchCondition;
import com.core.user.pojo.User;
import com.util.JSONUtil;
import com.util.framework.BaseController;

@Controller
public class SysLogController extends BaseController {
    protected Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private SysLogBo sysLogBo;

    public static final String URL_ENTITY = "/sysLog";
    public static final String URL_GOTO_MAIN_PAGE = URL_ENTITY + "/goto_main_page";
    public static final String URL_MAIN_SEARCH = URL_ENTITY + "/main_search";

    @RequestMapping(value = URL_GOTO_MAIN_PAGE, method = RequestMethod.GET)
    public ModelAndView gotoMainPage(SysLogSearchCondition usc, HttpSession session) {
        User userInfo = (User) session.getAttribute(CommonKeys.SESSION_USER);
        ModelAndView modelAndView = new ModelAndView("sysLog/sysLog_main");
        modelAndView.addObject(CommonJspKeys.JspParam_SessionUserInfo, userInfo);
        return modelAndView;
    }
    
    @ResponseBody
    @RequestMapping(value = URL_MAIN_SEARCH, method = RequestMethod.POST,produces={"application/json;charset=utf-8"})
    public String getSysLog(SysLogSearchCondition usc, HttpSession session) {
        String msg="";
        try {
            List<SysLogDto> sysLogList = sysLogBo.searchDto(usc);
            Integer totalRecordCount=sysLogBo.searchCnt(usc);
            if(sysLogList.size()==0){
            	 msg="{\"draw\":"+usc.getDraw()+",\"recordsTotal\":0,\"recordsFiltered\":0,\"data\": []}";
            }else{
            	msg=JSONUtil.toJSONstringNew(sysLogList);
            	//draw:请求次数，recordsTotal：总记录数,recordsFiltered:实际返回数，data:数据（json格式）
            	//datatable分页返回格式，
                msg="{\"draw\":"+usc.getDraw()+",\"recordsTotal\":"+totalRecordCount+",\"recordsFiltered\":"+totalRecordCount+",\"data\": "+msg+"}";
            }
            
        } catch (Exception e) {
            log.error("error", e);
            msg="{\"draw\":"+usc.getDraw()+",\"recordsTotal\":0,\"recordsFiltered\":0,\"data\": []}";
        }
        return msg;
    }

}
